<?php
   
/**
 * Mapper for tphotos
 * 
 * @author Martin Grossmann <snake.aas@gmail.com>
 * @version 1.0
 * @package Mappers
 *
 * @property int $idPhoto Unique id of photo
 * @property Album $Album Album to which picture belongs
 * @property string $Name Name of photo
 * @property string $Description Short description. 255 chars max
 * @property string $FileUNCPath Path to image. 20000 chars max
 * @property int $FileSize Size of image
 * @property MimeType $FileMimeType Mimetype of image.
 * @property string $FileMimeTypeName Name of mimetype of image.
 * @property int $FileWidth Width of the image
 * @property int $FileHeight Height of the image
 * @property DateTime $DateInserted Date insertion of image
 * @property string $ThumbnailUNC Path to the picture. 50 chars max
 * @property float $AverageRating 
 * @property int $RatingsCount
 * @property-read \Doctrine\Common\Collections\ArrayCollection $Parameters Parameters belonging to the photo
 * @property-read \Doctrine\Common\Collections\ArrayCollection $Comments Comments belonging to the photo
 * @property-read \Doctrine\Common\Collections\ArrayCollection $Ratings Ratings belonging to the photo
 * @property-read string $OrientationString Returns 'portrait' or 'landscape'
 * 
 * @entity
 * @table(name="tphotos", indexes={@index(name="TPhotos_TMimeTypes_FK", columns="FileMimeType")}, 
 *                        uniqueConstraints={@UniqueConstraint(name="idAlbum_name_unique", columns={"name", "idAlbum"})} )
 * @hasLifecycleCallbacks
 */     
class Photo {
  
   const THUMBNAIL_PATH = 'images/tphotos/';
   const ORIGINAL_PATH = 'photos/';
  
  /**
   * Unique id of photo
   * 
   * @access private
   * @var int
   * 
   * @Id 
   * @GeneratedValue
   * @column(name="idPhoto", type="integer")
   */
   private $idPhoto;
     
  
     
   /**
    * Name of photo
    * 
    * @access private
    * @var string 
    * 
    * @column(name="name", type="string", length=30)
    */
   private $name;
   
  /**
   * Short description
   * 
   * @access private
   * @var string
   * 
   * @column(name="Description", type="string", length=255, nullable=true)
   */
   private $description;
     
  /**
   * Path to image
   * 
   * @access private
   * @var string
   * 
   * @column(name="FileUNCPath", type="string", length=50)
   */
   private $fileUNCPath;
     
  /**
   * Size of image
   * 
   * @access private
   * @var int
   * 
   * @column(name="FileSize", type="integer")
   */
   private $fileSize;
     
  /**
   * Mimetype of image
   * 
   * @access private
   * @var MimeType
   * 
   * @manyToOne(targetEntity="MimeType")
   * @joinColumn(name="FileMimeType", referencedColumnName="Mime")
   */
   private $fileMimeType;
  
   /**
   * Name of mimetype of image
   * 
   * @access private
   * @var string
   * 
   * @column(name="FileMimeType", type="string", length=50)
   */
   private $fileMimeTypeName;
   
  /**
   * Width of the image
   * 
   * @access private
   * @var int
   * 
   * @column(name="FileWidth", type="smallint")
   */
   private $fileWidth;
     
  /**
   * Height of the image
   * 
   * @access private
   * @var int
   * 
   * @column(name="FileHeight", type="smallint")
   */
   private $fileHeight;
     
  /**
   * Date insertion of image
   * 
   * @access private
   * @var DateTime
   * 
   * @column(name="DateInserted", type="datetimetz")
   */
   private $dateInserted;
     
  /**
   * Binary data contains thumbnail of picture
   * 
   * @access private
   * @var string
   * 
   * @column(name="ThumbnailUNC", type="string", length="50", nullable=true)
   */
   private $thumbnailUNC;
   
   /**
    *
    * @access private
    * @var \Doctrine\Common\Collections\ArrayCollection 
    * 
    * @oneToMany(targetEntity="PhotosParameters", mappedBy="photo", cascade={"persist"})
    * 
    */
   private $parameters;
   
   /**
    *
    * @access private
    * @var \Doctrine\Common\Collections\ArrayCollection 
    * 
    * @oneToMany(targetEntity="Comment", mappedBy="photo")
    * @OrderBy({"insertionDate" = "DESC"})
    */
   private $comments;
   
   /**
    *
    * @access private
    * @var \Doctrine\Common\Collections\ArrayCollection 
    * 
    * @oneToMany(targetEntity="Rating", mappedBy="photo", cascade={"persist"})
    *      
    */
   private $ratings;
   
   /**
    *
    * @access private
    * @var float
    *  
    * @column(name="AverageRating", type="float")
    */
   private $averageRating; 
   
   /**
    *
    * @access private
    * @var type 
    * 
    * @column(name="RatingsCount", type="integer")
    */
   private $ratingsCount;
   
   /**
   * Album to which picture belongs
   * 
   * @access private
   * @var Album
   * 
   * @manyToOne(targetEntity="Album", inversedBy="photos")
   * @joinColumn(name="idAlbum", referencedColumnName="idAlbum")
   */
   private $album;
   
  public function __get($name) {
 
    switch($name) {
     
      case "idPhoto": 
        return $this->idPhoto;
        break;
    
      case "Album": 
        return $this->album;
        break;
    
      case "Name": 
        return $this->name;
        break;
     
      case "Description": 
        return $this->description;
        break;
    
      case "FileUNCPath": 
        return self::ORIGINAL_PATH.$this->fileUNCPath;
        break;
     
      case "FileSize": 
        return $this->fileSize;
        break;
     
      case "FileMimeType": 
        return $this->fileMimeType;
        break;
      
      case "FileMimeTypeName": 
        return $this->fileMimeTypeName;
        break;
     
      case "FileWidth": 
        return $this->fileWidth;
        break;
     
      case "FileHeight": 
        return $this->fileHeight;
        break;
    
      case "DateInserted": 
        return $this->dateInserted;
        break;
    
      case "ThumbnailUNC": 
        return self::THUMBNAIL_PATH.$this->thumbnailUNC;
        break;
      
      case "Parameters": 
        return $this->parameters;
        break;
      
      case "Comments":
        return $this->comments;
        break;
      
      case "Ratings":
        return $this->ratings;
        break;
      
      case "AverageRating":
        return round($this->averageRating,1);
        break;
      
      case "RatingsCount":
        return $this->ratingsCount;
        break;
      
      case "OrientationString":
        return ($this->fileWidth < $this->fileHeight) ? 'portrait' : 'landscape';
      
      default: 
        throw new EntityException(sprintf(T_("Trying to get undefined property")." ", $name));
    }
  }
  
  public function __set($name, $value) {

    switch($name) {
      case "idPhoto": 
        $this->idPhoto = Validations::validateInteger($value, 0, null, false);
        break; 

      case "Album": 
        $this->album = Validations::ValidateObject($value, 'Album');
        break; 
      
      case "Name": 
        $this->name = Validations::validateString($value, 30, null, false);
        break; 

      case "Description": 
        $this->description = Validations::validateString($value, 255, null, true);
        break; 

      case "FileUNCPath": 
        $position = strpos($value, self::ORIGINAL_PATH);
        if ($position !== false) {
          $value = str_replace(self::ORIGINAL_PATH, '', $value);
        } 
        $this->fileUNCPath = Validations::validateString($value, 50, null, false);
        break; 

      case "FileSize": 
        $this->fileSize = Validations::validateInteger($value, 0, null, false);
        break; 

      case "FileMimeTypeName": 
        $this->fileMimeTypeName = Validations::validateString($value, 15, Validations::regexp_MimeType, false);
        break; 

      case "FileMimeType": 
        $this->fileMimeType = Validations::ValidateObject($value, 'MimeType');
        $this->fileMimeTypeName = $value->Name;
        break; 
      
      case "FileWidth": 
        $this->fileWidth = Validations::validateInteger($value, 0, null, false);
        break; 

      case "FileHeight": 
        $this->fileHeight = Validations::validateInteger($value, 0, null, false);
        break; 

      case "ThumbnailUNC": 
        $position = strpos($value, self::THUMBNAIL_PATH);
        if ($position !== false) {
          $value = str_replace(self::THUMBNAIL_PATH, '', $value);
        }          
        $this->thumbnailUNC = Validations::validateString($value, 50, null, true);
        break; 

      case "AverageRating":
        $this->averageRating = Validations::ValidateFloat($value);
        break;
      
      case "RatingsCount":
        $this->ratingsCount = Validations::validateInteger($value, 0);
        break;
    
      default: 
        throw new EntityException(sprintf(T_("Trying to set undefined property")." ", $name));
    }
  }  
  
  public function __construct() {
    $this->parameters = new \Doctrine\Common\Collections\ArrayCollection();
    $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   *
   * @access public
   * @param PhotosParameters $param 
   * @return void
   */
  public function addParameter(PhotosParameters $param) {
    $this->parameters->add($param);
    $param->Photo = $this;
  }
  
  /**
   *
   * @access public
   * @param Comment $comment 
   * @return void
   */
  public function addComment(Comment $comment) {
    $this->comments->add($comment);
    $comment->Photo = $this;
  }
  
  /**
   *
   * @access public
   * @param Rating $rating 
   * @return void
   */
  public function addRating(Rating $rating) {
    $this->ratings->add($rating);
    $rating->Photo = $this;
  }
  
  /**
   * @prePersist
   */
  public function preInsert() {
    $this->dateInserted = new DateTime('now');
  }
  
  /**
   * @preRemove
   */
  public function preRemove() {
    --$this->Album->PhotoCount;
    @unlink($_SERVER["DOCUMENT_ROOT"].WebRoot.self::ORIGINAL_PATH.$this->fileUNCPath);
    @unlink($_SERVER["DOCUMENT_ROOT"].WebRoot.self::THUMBNAIL_PATH.$this->thumbnailUNC);
  }

}